﻿//$(function () {
//    $("#txt_fdate").datepicker({ dateFormat: 'dd/mm/yy' });
//    $("#txt_tdate").datepicker({ dateFormat: 'dd/mm/yy' });
//});

function ClientSideRenderReportTable() {
    if (!CheckValidCondition()) return;

    var FromDate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var ToDate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
    var PermitType = $("#_legalDrugPermitType").val();
    var MinistryId = $("#_legalDrugPermitOfMinistry").val();
    var CompanyName = $("#_legalDrugPermitCompanyName").val();
    var DrugName = $("#_legalDrugPermitDrugName").val();

    var tableString = Portal.SODC.IDIS.LawDrugInformation.ServerSideRenderReportTable(FromDate, ToDate, MinistryId, PermitType, CompanyName, DrugName).value;
    if (tableString.indexOf("ERROR") != -1) {
        alert(tableString.replace("ERROR:","").trim());
        return;
    }
    $("#divStatisticResult").html(tableString);
}

function ClientSideExportData() {
    if (!CheckValidCondition()) return;

    var FromDate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var ToDate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
    var PermitType = $("#_legalDrugPermitType").val();
    var MinistryId = $("#_legalDrugPermitOfMinistry").val();
    var CompanyName = $("#_legalDrugPermitCompanyName").val();
    var DrugName = $("#_legalDrugPermitDrugName").val();

    var rValue = Portal.SODC.IDIS.LawDrugInformation.ServerSideRenderExcel(FromDate, ToDate, MinistryId, PermitType, CompanyName, DrugName).value;
    if (rValue.indexOf('ERROR') != -1) {
        alert(rValue.replace("ERROR:", "").trim());
    }
    else if (rValue.indexOf('NONE') != -1) {
        alert('Không tồn tại dữ liệu để in ấn.');
    }
    else
        window.open(rValue, '_blank');
}

function CheckValidCondition() {
    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    fromyear = document.getElementById("txt_fdate").value;
//    toyear = document.getElementById("txt_tdate").value;
    if (fromyear.length == 0 || toyear == 0) {
        alert("Bạn phải chọn thời gian để tạo thống kê!")
        return false;
    }
    return true;
}